package com.dubbo.cp.consumer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import javax.websocket.Session;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.cp.IService.UserService;
import com.dubbo.cp.entity.UserEntity;
import com.dubbo.cp.uitls.Pager;

/**
 * @author 林景锋
 *
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Reference
    private UserService userServiceImpl;
    
    private Pager<UserEntity>pager;
    
    @RequestMapping(value="uList")
    public String userList(Model model) {
    	
    	List<UserEntity> useList =  userServiceImpl.getUserList();
    	model.addAttribute("userList",useList);
    	System.out.println("userList"+useList);
    	return "userList";
    	
    	
    }
    /*
     * 
     * 分页查询用户信息
     */
    @RequestMapping(value="findUserByPage")
    public ModelAndView findByPage(Model model,String currentPage) {
    	
    	System.out.println("进入分页查询");
    	int pageSize = Integer.parseInt(currentPage);
    	if(pager==null) {
    		pager = new Pager<UserEntity>();
    	}
    	pager.setCurrentPage(pageSize);//设置分页的当长度
    	int length = pager.getCurrentPage();
    	int recordSize = pager.getRecordSize();
    	int start = (length-1)*recordSize;
    	pager.setList(userServiceImpl.getUserList());//先查询全部的
    	pager.initTotalPage();//初始化总页数
    	List<UserEntity>ulist = userServiceImpl.getUserByPage(start, pager.getRecordSize());
    	model.addAttribute("ulist", ulist);
    	model.addAttribute("pager", pager);
    	ModelAndView modelAndView = new ModelAndView();
    	modelAndView.setViewName("/userList");
    	return modelAndView;
    	
		
    	
    }
    
    /**
     * 
     * 
     * 添加用户跳转
     * @return
     */
    @RequestMapping(value="inputUser")
    public String inputUser() {
		return "userAdd";
    	
    }
    
    @PostMapping(value="insertUser")
    public String insertUser(@Valid UserEntity userEntity) {
    	System.out.println("添加");
    	System.out.println(userEntity.getUsername());
//    	if(userEntity==null) {
//    		return "redirect:inputUser";
//    	}
    	userServiceImpl.InsertUserEntity1(userEntity);
    	
		return "redirect:findUserByPage?currentPage=1";
    	
    }
    /**
     * 编辑跳转
     * @return
     */
    
	@RequestMapping(value="editUser")
    public ModelAndView editUser(Model model ,@RequestParam int id) {
    	System.out.println("id===="+id);
    	
    	
		UserEntity userEntity = userServiceImpl.findUserByUserid(id);
		//Map<String, Object> resMap = new HashMap<String, Object>();
		model.addAttribute("userEntity", userEntity);
//    	session.put("userEntity", userEntity);
		System.out.println("userEntity" + userEntity.getId()+userEntity.getIdcard());
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/userEdit");
		return modelAndView;
    	
    }
	@PostMapping(value="updateUser")
	public String updateUser( UserEntity userEntity) {
		
		
		System.out.println("sssss"+userEntity.getUsername());
		System.out.println("dddddd==="+userEntity.getAddress());
	    userServiceImpl.updateUserEntity(userEntity);
		return "redirect:findUserByPage?currentPage=1";
		
		
	}
	@RequestMapping(value="deleteUser")
    public String deleteUser(@RequestParam int id) {
    	System.out.println(id);
    	userServiceImpl.deleteUserEntity(id);
		return "redirect:findUserByPage?currentPage=1";
    	
    }
    
}
